#include "lightoptionswidget.h"
#include "ui_lightoptionswidget.h"

LightOptionsWidget::LightOptionsWidget(Light &l, bool advanced, QWidget *parent) :
  QWidget(parent),
  ui(new Ui::LightOptionsWidget)
{
  ui->setupUi(this);

  connect(ui->redSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateRed(int)));
  connect(ui->redBox, SIGNAL(valueChanged(int)), this, SLOT(UpdateRed(int)));
  connect(ui->greenSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateGreen(int)));
  connect(ui->greenBox, SIGNAL(valueChanged(int)), this, SLOT(UpdateGreen(int)));
  connect(ui->blueSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateBlue(int)));
  connect(ui->blueBox, SIGNAL(valueChanged(int)), this, SLOT(UpdateBlue(int)));
  connect(ui->cutoffSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateCutoff(int)));
  connect(ui->cutoffBox, SIGNAL(valueChanged(int)), this, SLOT(UpdateCutoff(int)));
  connect(ui->exponentSlider, SIGNAL(valueChanged(int)), this, SLOT(UpdateExponent(int)));
  connect(ui->exponentBox, SIGNAL(valueChanged(int)), this, SLOT(UpdateExponent(int)));

  light = &l;
  ui->redSlider->setValue(light->color[0] * 255);
  ui->redBox->setValue(light->color[0] * 255);
  ui->greenSlider->setValue(light->color[1] * 255);
  ui->greenBox->setValue(light->color[1] * 255);
  ui->blueSlider->setValue(light->color[2] * 255);
  ui->blueBox->setValue(light->color[2] * 255);
  ui->cutoffSlider->setValue(light->cutoff);
  ui->cutoffBox->setValue(light->cutoff);
  ui->exponentSlider->setValue(light->exponent);
  ui->exponentBox->setValue(light->exponent);

  if (!advanced) {
    ui->cutoff->setDisabled(true);
    ui->cutoffBox->setDisabled(true);
    ui->cutoffSlider->setDisabled(true);
    ui->exponent->setDisabled(true);
    ui->exponentBox->setDisabled(true);
    ui->exponentSlider->setDisabled(true);
  }
}

LightOptionsWidget::~LightOptionsWidget()
{
  delete ui;
}

void LightOptionsWidget::UpdateRed(int value) {
  light->changed = true;
  light->color[0] = value / 255.f;
  ui->redSlider->setValue(value);
  ui->redBox->setValue(value);
}

void LightOptionsWidget::UpdateGreen(int value) {
  light->changed = true;
  light->color[1] = value / 255.f;
  ui->greenSlider->setValue(value);
  ui->greenBox->setValue(value);
}

void LightOptionsWidget::UpdateBlue(int value) {
  light->changed = true;
  light->color[2] = value / 255.f;
  ui->blueSlider->setValue(value);
  ui->blueBox->setValue(value);
}

void LightOptionsWidget::UpdateCutoff(int value) {
  light->changed = true;
  light->cutoff = value;
  ui->cutoffSlider->setValue(value);
  ui->cutoffBox->setValue(value);
}

void LightOptionsWidget::UpdateExponent(int value) {
  light->changed = true;
  light->exponent = value;
  ui->exponentSlider->setValue(value);
  ui->exponentBox->setValue(value);
}
